/*
 * @(#)IndexTag.java $version 2012-3-5
 * 
 * Copyright 2011 RogueDevelopment Gruop. All rights Reserved.
 * ROGUEDEVELOPMENT PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nado.extensions.pagination;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

/**
 * @author LEESUNG
 *
 */
public class IndexTag extends PageTag {
	@SuppressWarnings("unchecked")
	protected String render(PaginationInfo info, PaginationTemplate tmpl)
	  {
	    Map params = new HashMap();

	    StringBuilder builder = new StringBuilder();
	    for (int i = info.getFirstPageInIndex(); i <= info.getLastPageInIndex(); ++i) {
	      params.put("LINK", info.getUrlFor(i));
	      params.put("PAGE", Integer.toString(i));

	      boolean isCurrent = i == info.getPage();
	      boolean isFirst = i == info.getFirstPageInIndex();
	      boolean isLast = i == info.getLastPageInIndex();
	      String templateString;
	      if (isCurrent) {
	        if (isLast)
	          templateString = tmpl.getIndexLastCurrent();
	        else if (isFirst)
	          templateString = tmpl.getIndexFirstCurrent();
	        else
	          templateString = tmpl.getIndexCurrent();
	      }
	      else if (isFirst)
	        templateString = tmpl.getIndexFirst();
	      else if (isLast)
	        templateString = tmpl.getIndexLast();
	      else {
	        templateString = tmpl.getIndex();
	      }

	      builder.append(PaginationTemplate.process(templateString, params));

	      if ((i < info.getLastPageInIndex()) && (StringUtils.isNotEmpty(tmpl.getSeparator()))) {
	        builder.append(tmpl.getSeparator());
	      }
	    }

	    return builder.toString();
	  }
}
